package com.zzl.study.nacos.registry;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.zzl.study.nacos.event.NacosDiscoveryInfoChangedEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.event.EventListener;
import org.springframework.util.Assert;

/**
 * Nacos多注册中心自动注册器
 *
 * @author: zhangzl
 * @date: 2025/7/15 19:25
 * @version: 1.0
 */
@Slf4j
public class NacosAutoServiceMultiRegistration extends AbstractAutoServiceRegistration<Registration> {

    /**
     * Nacos多注册中心注册信息
     */
    private final NacosMultiRegistration registration;

    /**
     * 构造方法
     *
     * @param serviceRegistry                   服务注册中心
     * @param autoServiceRegistrationProperties 自动注册属性配置
     * @param registration                      Nacos多注册中心注册信息
     */
    public NacosAutoServiceMultiRegistration(ServiceRegistry<Registration> serviceRegistry, AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosMultiRegistration registration) {
        super(serviceRegistry, autoServiceRegistrationProperties);
        this.registration = registration;
    }

    @Deprecated
    public void setPort(int port) {
        getPort().set(port);
    }

    @Override
    protected NacosMultiRegistration getRegistration() {
        if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
            this.registration.setPort(this.getPort().get());
        }
        Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
        return this.registration;
    }

    @Override
    protected NacosMultiRegistration getManagementRegistration() {
        return null;
    }

    @Override
    protected void register() {
        if (!this.registration.getNacosMultiDiscoveryProperties().isRegisterEnabled()) {
            log.debug("Registration disabled.");
            return;
        }
        if (this.registration.getPort() < 0) {
            this.registration.setPort(getPort().get());
        }
        super.register();
    }

    @Override
    protected void registerManagement() {
        if (!this.registration.getNacosMultiDiscoveryProperties().isRegisterEnabled()) {
            return;
        }
        super.registerManagement();
    }

    @Override
    protected Object getConfiguration() {
        return this.registration.getNacosMultiDiscoveryProperties();
    }

    @Override
    protected boolean isEnabled() {
        return this.registration.getNacosMultiDiscoveryProperties().isRegisterEnabled();
    }

    @Override
    @SuppressWarnings("deprecation")
    protected String getAppName() {
        String appName = registration.getNacosMultiDiscoveryProperties().getService();
        return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
    }

    @EventListener
    public void onNacosDiscoveryInfoChangedEvent(NacosDiscoveryInfoChangedEvent event) {
        restart();
    }

    /**
     * 重新启动
     */
    private void restart() {
        this.stop();
        this.start();
    }

}
